home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 21 / CU Amiga Magazine's Super CD-ROM 21 (1998)(EMAP Images)(GB)[!][issue 1998-04].iso / CUCD / Utilities / DTConvert / DTConvert.c < prev    next >
C/C++ Source or Header  |  1998-02-05  |  54KB  |  1,758 lines

  1.  
  2. /*
  3. **
  4. **  $VER: DTConvert.c 1.6 (5.2.98)
  5. **  datatypes.library/Examples/DTConvert
  6. **
  7. **  Converts file into another format using datatypes
  8. **
  9. **  Main program and startup code
  10. **
  11. **  Written 1996-1998 by Roland 'Gizzy' Mainz
  12. **
  13. */
  14.  
  15. /* project includes */
  16. #include "DTConvert.h"
  17. #include "gui.h"
  18.  
  19. /* version string */
  20. #include "DTConvert_rev.h"
  21.  
  22. /*****************************************************************************/
  23.  
  24. /* list of datatypes, used in gadtools listview gadget */
  25. struct DTList
  26. {
  27.     struct List dtl_List;
  28.     UWORD       dtl_NumNodes;
  29.     APTR        dtl_Pool;
  30. };
  31.  
  32. /* single dtlist node */
  33. struct DTNode
  34. {
  35.     struct Node dtn_Node;
  36.     STRPTR      dtn_DTName;
  37.     /* dynamic bufferspace follows here */
  38. };
  39.  
  40.  
  41. /*****************************************************************************/
  42. /* shared libraries */
  43.  
  44. struct Library       *UtilityBase    = NULL;
  45. struct GfxBase       *GfxBase        = NULL;
  46. struct IntuitionBase *IntuitionBase  = NULL;
  47. struct Library       *GadToolsBase   = NULL;
  48. struct Library       *DataTypesBase  = NULL;
  49. struct Library       *AslBase        = NULL;
  50. struct Library       *IconBase       = NULL;
  51. struct Library       *WorkbenchBase  = NULL;
  52.  
  53. /*****************************************************************************/
  54.  
  55. /* version_string */
  56. STRPTR versionstring = VERSTAG;
  57.  
  58. /*****************************************************************************/
  59.  
  60. /* local prototypes */
  61. static                 void                 DefaultSettings( void );
  62. static                 void                 ClearRDA( void );
  63. static                 void                 ScanRDA( void );
  64. static                 void                 ScanToolTypes( TEXT ** );
  65. static                 void                 FreeInitProjectResult( void );
  66. static                 void                 ReadENVPrefs( void );
  67.  
  68. static                 BOOL                 CreateBasicResources( void );
  69. static                 void                 DeleteBasicResources( void );
  70.  
  71. static                 BOOL                 OpenLibStuff( void );
  72. static                 void                 CloseLibStuff( void );
  73.  
  74. static                 void                 RunTool( STRPTR );
  75.  
  76. static                 BOOL                 NewProject( STRPTR, STRPTR, BOOL, STRPTR, BOOL, STRPTR );
  77.  
  78. static                 struct DTList       *CreateDTDescrList( BPTR, ULONG );
  79. static                 void                 FreeDTDescrList( struct DTList * );
  80. static                 struct DTNode       *FindDTNodeByName( struct DTList *, STRPTR );
  81.  
  82. static                 void                 LockGUI( void );
  83. static                 void                 UnlockGUI( void );
  84.  
  85. static                 STRPTR               StringSave( STRPTR );
  86. static                 STRPTR               AllocStringBuff( ULONG );
  87. static                 STRPTR               UpdateString( STRPTR *, STRPTR );
  88. static                 void                 FreeString( STRPTR );
  89.  
  90. static                 struct DiskObject   *GetToolDiskObject( STRPTR );
  91.  
  92. static                 ULONG                SumString( STRPTR );
  93.  
  94.  
  95. /*****************************************************************************/
  96.  
  97. long main_retval,
  98.      main_retval2;
  99.  
  100. static struct RDArgs  *startuprda;
  101.  
  102. /* mempool for strings */
  103. static APTR StringPool;
  104.  
  105. /*****************************************************************************/
  106.  
  107. /* template for ReadArgs */
  108. #define STARTUP_TEMPLATE "FROM=NAME=SRCNAME,"          \
  109.                          "DESTDATATYPE=DATATYPE=DTN,"  \
  110.                          "IFF/S,"                      \
  111.                          "TO=DESTNAME,"                \
  112.                          "GUI/S,"                      \
  113.                          "PUBSCREEN/K"
  114.  
  115. #define ENV_TEMPLATE STARTUP_TEMPLATE /* equal because there are no /A switches */
  116.  
  117. static
  118. struct
  119. {
  120.     STRPTR  srcname;
  121.     STRPTR  destdatatype;
  122.     long   *iff;
  123.     STRPTR  destname;
  124.     long   *gui;
  125.     STRPTR  pubscreen;
  126. } result;
  127.  
  128. static
  129. struct
  130. {
  131.     STRPTR  srcname;
  132.     STRPTR  destdatatype;
  133.     BOOL    iff;
  134.     STRPTR  destname;
  135.     BOOL    gui;
  136.     STRPTR  pubscreen;
  137. } project;
  138.  
  139. /*****************************************************************************/
  140.  
  141. /****** DTConvert/DTConvert **************************************************
  142. *
  143. *    NAME
  144. *       DTConvert -- DataTypes-based conversion tool
  145. *
  146. *    FORMAT
  147. *       DTConvert [FROM|NAME|SRCNAME <file>]
  148. *                 [DESTDATATYPE|DATATYPE|DTN <datatype>] [IFF]
  149. *                 [TO|DESTNAME <file>] [GUI] [PUBSCREEN <public screen>]
  150. *
  151. *    TEMPLATE
  152. *       FROM=NAME=SRCNAME,DESTDATATYPE=DATATYPE=DTN,IFF/S,TO=DESTNAME,
  153. *       GUI/S,PUBSCREEN/K
  154. *
  155. *    PURPOSE
  156. *       Convert data from one format into another format using datatypes
  157. *       classes.
  158. *
  159. *    DESCRIPTION
  160. *       DTConvert converts data from one format into another format,
  161. *       e.g. picture -> picture,
  162. *            animation -> anmation,
  163. *            movie -> movie,
  164. *            sound -> sound,
  165. *            text -> text (n/a)
  166. *
  167. *      You simply have to set the souce file name (SRCNAME), the
  168. *      datatype to convert to (DATATYPE) (or the IFF switch if you want to
  169. *      convert into the base-IFF format and the destination file name (TO).
  170. *
  171. *    BUGS
  172. *      - picture.datatype V43 interface not implemented.
  173. *        This causes that any encoder only runs in V42 mode any may
  174. *        only encode 8 bit data.
  175. *
  176. *      - path names are limitted to 1024 chars, larger filenames may cause
  177. *        mailfunctions
  178. *
  179. *    NOTES
  180. *      - picture.datatype V43 pixmap interface not supported.
  181. *
  182. *      - text conversion not supported yet.
  183. *
  184. *    TODO
  185. *      - picture.datatype V43 compatibility
  186. *
  187. *      - text conversion
  188. *
  189. *      - GUI and WB suppport
  190. *
  191. *    HISTORY
  192. *       V1.1:
  193. *         - First release to dta@amigawolrd.com mailinglist.
  194. *
  195. *       V1.2:
  196. *         - Added support for animation.datatype subclasses
  197. *
  198. *       V1.3:
  199. *         - Minor fixes.
  200. *
  201. *       V1.4
  202. *         - Added support for sound.datatype V40 subclasses, partial support
  203. *           for suggested sound.datatype V41 interface.
  204. *
  205. *         - Added some usefull comments.
  206. *
  207. *         - WriteAnimClass/WriteSoundClass now sets
  208. *           SetIoErr( ERROR_REQUIRED_ARGUMENT_MISSING ); if something goes
  209. *           wrong (this should abort the encoder; only ERROR_OBJECT_NOT_FOUND
  210. *           is accepted here).
  211. *
  212. *         - Fixed some holes in the error handling.
  213. *
  214. *         - GUI added.
  215. *
  216. *         - Added WB support
  217. *
  218. *         - Added ENV variable support; a local or global (ENV) variable
  219. *           "DTConvert" takes the same arguments as the shell template.
  220. *           Variable settings can be overridden by any argument.
  221. *
  222. *         - Project split in seperate sources (e.g. main, GUI, converters)
  223. *
  224. *       V1.5
  225. *         - Added the feature that the datatypes selection requester shows
  226. *           only entries which match the source group IDs
  227. *           (if a source has been already selected).
  228. *
  229. *         - Added kluge which alows the conversion between GID_MOVIE
  230. *           and GID_ANIMATION datatypes (both are based on
  231. *           animation.datatype, here we have the same interface :-)
  232. *
  233. *         - Fixed the bug that ConvertAnimation function did not deal
  234. *           with truecolor bitmaps (e.g. CyberGFX bitmaps for example,
  235. *           truecolor bitmaps are indicated by ADTA_NumColors == 0.
  236. *           Fixed.
  237. *
  238. *       V1.6
  239. *         - Fixed the bug that ObtainDataTypeA was called for each
  240. *           INTUITICK if the datatype selection requester was open.
  241. *           Fixed.
  242. *
  243. *
  244. *    AUTHOR's REQUEST
  245. *        By  releasing  this program I do  not  place any obligations on you,
  246. *        feel free to share this program with your  friends (and enemies).
  247. *
  248. *        If you want to blame me, report any bugs, or wants a new version
  249. *        send your letter to:
  250. *                        Roland Mainz
  251. *                        Hohenstaufenstraße 8
  252. *                        52388 Nörvenich
  253. *                        GERMANY
  254. *
  255. *        Phone: (+49)(0)2426/901568
  256. *        Fax:   (+49)(0)2426/901569
  257. *
  258. *        EMAIL is also available:
  259. *        GISBURN@w-specht.rhein-ruhr.de
  260. *
  261. *        If you want to send me attachments larger than 1MB (up to 5MB,
  262. *        more with my permission):
  263. *        Up to April 1998 I'm reachable using this email address, too:
  264. *        Reinhold.A.Mainz@KBV.DE
  265. *
  266. *        | Please put your name and address in your mails !
  267. *        | German mailers should add their phone numbers.
  268. *        | See BUGS section above when submitting bug reports.
  269. *
  270. *        Sorry, but I can only look once a week for mails.
  271. *        If you don't hear something from me within three weeks, please
  272. *        send your mail again (but watch about new releases) (problems with
  273. *        this email port are caused by reconfigurations, hackers, network
  274. *        problems etc.).
  275. *
  276. *        The  entire  "DTConvert"  package may  be  noncommercially
  277. *        redistributed, provided  that  the package  is always  distributed
  278. *        in it's complete  form (including it's documentation).  A small copy
  279. *        fee for media costs is okay but any kind of commercial distribution
  280. *        is strictly forbidden! Comments  and  suggestions  how  to  improve
  281. *        this program  are generally appreciated!
  282. *
  283. *        Thanks to Matt Dillon for his DICE, David Junod for this datatypes
  284. *        environment and Olaf 'Olsen' Barthel for his help, ideas and some
  285. *        text clips from his documentations.
  286. *
  287. *    SEE ALSO
  288. *
  289. ******************************************************************************
  290. *
  291. */
  292.  
  293.  
  294. #ifdef _DCC
  295. /* disable CTRL_C break support (DICE CTRL_C abort function) */
  296. void chkabort( void )
  297. {
  298. }
  299.  
  300.  
  301. /* DICE workbench entry */
  302. long wbmain( struct WBStartup *wbstartup )
  303. {
  304.     /* Call main like SAS-C */
  305.     return( (int)main( 0L, (STRPTR *)wbstartup ) );
  306. }
  307. #endif /* _DCC */
  308.  
  309.  
  310. int main( int ac, STRPTR *av )
  311. {
  312.     LONG               numArgs,
  313.                        x;
  314.     struct WBStartup  *wbstartup;
  315.     struct WBArg      *wbarg;
  316.     struct DiskObject *tooldobj,
  317.                       *projectdobj;
  318.     BPTR               oldToolLock    = NULL,
  319.                        oldProjectLock = NULL;
  320.  
  321.     main_retval2 = 0L;
  322.     main_retval  = RETURN_OK;
  323.  
  324.     if( CreateBasicResources() )
  325.     {
  326.       DefaultSettings();
  327.  
  328.       /* check if something goes wrong (ENV error etc.) */
  329.       if( main_retval == RETURN_OK )
  330.       {
  331. /* Workbench */
  332.         if( ac == 0L )
  333.         {
  334.           wbstartup = (struct WBStartup *)av;
  335.  
  336.           numArgs = wbstartup -> sm_NumArgs;
  337.           wbarg   = wbstartup -> sm_ArgList;
  338.  
  339.           if( *(wbarg[ 0 ] . wa_Name) )
  340.           {
  341.             if( wbarg[ 0 ] . wa_Lock )
  342.             {
  343.               oldToolLock = CurrentDir( (wbarg[ 0 ] . wa_Lock) );
  344.             }
  345.  
  346.             if( tooldobj = GetToolDiskObject( (wbarg[ 0 ] . wa_Name) ) )
  347.             {
  348.               /* two possible cases when started from workbench ... */
  349.               if( numArgs < 2L )
  350.               {
  351.                 /* ... first case, only our tool icon is given, create one project here */
  352.  
  353.                 ScanToolTypes( (STRPTR *)(tooldobj -> do_ToolTypes) );
  354.  
  355.                 /* check if something goes wrong (parsing error, etc.) */
  356.                 if( main_retval == RETURN_OK )
  357.                 {
  358.                   RunTool( NULL );
  359.                 }
  360.  
  361.                 FreeInitProjectResult();
  362.               }
  363.               else
  364.               {
  365.                 /* ... second case, a couple of project icons are given, multiple projects will start from here */
  366.                 for( x = 1L ; x < numArgs ; x++ )
  367.                 {
  368.                   if( wbarg[ x ] . wa_Lock )
  369.                   {
  370.                     oldProjectLock = CurrentDir( (wbarg[ x ] . wa_Lock) );
  371.                   }
  372.  
  373.                   if( *(wbarg[ x ] . wa_Name) )
  374.                   {
  375.                     if( projectdobj = GetDiskObject( (wbarg[ x ] . wa_Name) ) )
  376.                     {
  377.                       ScanToolTypes( (STRPTR *)(tooldobj -> do_ToolTypes) );
  378.                       ScanToolTypes( (STRPTR *)(projectdobj -> do_ToolTypes) );
  379.  
  380.                       /* check if something goes wrong (parsing error, etc.) */
  381.                       if( main_retval == RETURN_OK )
  382.                       {
  383.                         RunTool( (wbarg[ x ] . wa_Name) );
  384.                       }
  385.  
  386.                       FreeInitProjectResult();
  387.                       DefaultSettings();
  388.  
  389.                       FreeDiskObject( projectdobj );
  390.                     }
  391.                   }
  392.  
  393.                   if( wbarg[ x ] . wa_Lock )
  394.                   {
  395.                     CurrentDir( oldProjectLock );
  396.                   }
  397.                 }
  398.               }
  399.  
  400.               FreeDiskObject( tooldobj );
  401.             }
  402.  
  403.             if( wbarg[ 0 ] . wa_Lock )
  404.             {
  405.               CurrentDir( oldToolLock );
  406.             }
  407.           }
  408.         }
  409.         else
  410.         {
  411. /* CLI/Shell */
  412.           if( startuprda = ReadArgs( STARTUP_TEMPLATE, (LONG *)(&result), NULL ) )
  413.           {
  414.             /* did we get a CTRL_C signal ? */
  415.             if( !CheckSignal( SIGBREAKF_CTRL_C ) )
  416.             {
  417.               ScanRDA();
  418.  
  419.               /* check if something goes wrong (parsing error, etc.) */
  420.               if( main_retval == RETURN_OK )
  421.               {
  422.                 RunTool( NULL );
  423.               }
  424.  
  425.               FreeInitProjectResult();
  426.             }
  427.             else
  428.             {
  429.               main_retval2 = ERROR_BREAK;
  430.               main_retval  = RETURN_WARN;
  431.             }
  432.  
  433.             FreeArgs( startuprda );
  434.           }
  435.           else
  436.           {
  437.             main_retval2 = IoErr();
  438.             main_retval  = RETURN_FAIL;
  439.           }
  440.         }
  441.       }
  442.  
  443.       DeleteBasicResources();
  444.     }
  445.  
  446.     PrintFault( main_retval2, NAME );
  447.  
  448.     SetIoErr( main_retval2 );
  449.  
  450.     return( main_retval );
  451. }
  452.  
  453.  
  454. static
  455. void DefaultSettings( void )
  456. {
  457.     ClearRDA();
  458.  
  459.     memset( (void *)(&project), 0, sizeof( project ) );
  460.  
  461.     ReadENVPrefs();
  462. }
  463.  
  464.  
  465. static
  466. void ReadENVPrefs( void )
  467. {
  468.     struct RDArgs envvarrda =
  469.     {
  470.       NULL,
  471.       256L,
  472.       0L,
  473.       0L,
  474.       NULL,
  475.       0L,
  476.       NULL,
  477.       RDAF_NOPROMPT
  478.     };
  479.  
  480.     TEXT varbuff[ 258 ];
  481.  
  482.     envvarrda . RDA_Source . CS_Buffer = varbuff;
  483.  
  484.     if( GetVar( NAME, varbuff, 256L, 0UL ) != (-1L) )
  485.     {
  486.       strcat( varbuff, "\n" );
  487.  
  488.       if( ReadArgs( ENV_TEMPLATE, (LONG *)(&result), (&envvarrda) ) )
  489.       {
  490.         ScanRDA();
  491.  
  492.         FreeArgs( (&envvarrda) );
  493.       }
  494.       else
  495.       {
  496.         main_retval2 = IoErr();
  497.         main_retval  = RETURN_FAIL;
  498.       }
  499.     }
  500. }
  501.  
  502.  
  503. static
  504. void ClearRDA( void )
  505. {
  506.     memset( (void *)(&result), 0, sizeof( result ) );
  507. }
  508.  
  509.  
  510. static
  511. void ScanRDA( void )
  512. {
  513.     if( result . srcname )
  514.     {
  515.       UpdateString( (&(project . srcname)), (result . srcname) );
  516.     }
  517.  
  518.     if( result . destdatatype )
  519.     {
  520.       UpdateString( (&(project . destdatatype)), (result . destdatatype) );
  521.     }
  522.  
  523.     if( result . iff )
  524.     {
  525.       project . iff = TRUE;
  526.     }
  527.  
  528.     if( result . destname )
  529.     {
  530.       UpdateString( (&(project . destname)), (result . destname) );
  531.     }
  532.  
  533.     if( result . gui )
  534.     {
  535.       project . gui = TRUE;
  536.     }
  537.  
  538.     if( result . pubscreen )
  539.     {
  540.       UpdateString( (&(project . pubscreen)), (result . pubscreen) );
  541.     }
  542.  
  543.     ClearRDA();
  544. }
  545.  
  546.  
  547. static
  548. void ScanToolTypes( TEXT **tt )
  549. {
  550.     STRPTR s;
  551.  
  552.     if( s = FindToolType( tt, "FROM" ) )
  553.       result . srcname = s;
  554.  
  555.     if( s = FindToolType( tt, "NAME" ) )
  556.       result . srcname = s;
  557.  
  558.     if( s = FindToolType( tt, "SRCNAME" ) )
  559.       result . srcname = s;
  560.  
  561.     if( s = FindToolType( tt, "DESTDATATYPE" ) )
  562.       result . destdatatype = s;
  563.  
  564.     if( s = FindToolType( tt, "DATATYPE" ) )
  565.       result . destdatatype = s;
  566.  
  567.     if( s = FindToolType( tt, "DTN" ) )
  568.       result . destdatatype = s;
  569.  
  570.     if( s = FindToolType( tt, "IFF" ) )
  571.       result . iff = (long *)s;
  572.  
  573.     if( s = FindToolType( tt, "TO" ) )
  574.       result . destname = s;
  575.  
  576.     if( s = FindToolType( tt, "DESTNAME" ) )
  577.       result . destname = s;
  578.  
  579.     if( s = FindToolType( tt, "GUI" ) )
  580.       result . gui = (long *)s;
  581.  
  582.     if( s = FindToolType( tt, "PUBSCREEN" ) )
  583.       result . pubscreen = s;
  584.  
  585.     ScanRDA();
  586. }
  587.  
  588.  
  589. static
  590. void FreeInitProjectResult( void )
  591. {
  592.     FreeString( (project . srcname) );
  593.     FreeString( (project . destdatatype) );
  594.     FreeString( (project . destname) );
  595.     FreeString( (project . pubscreen) );
  596. }
  597.  
  598.  
  599. static
  600. BOOL CreateBasicResources( void )
  601. {
  602.     if( OpenLibStuff() )
  603.     {
  604.       if( StringPool = CreatePool( (MEMF_PUBLIC | MEMF_CLEAR), 128UL, 128UL ) )
  605.       {
  606.         return( TRUE );
  607.  
  608. #ifdef COMMENTED_OUT
  609.         DeletePool( StringPool );
  610. #endif /* COMMENTED_OUT */
  611.       }
  612.  
  613.       CloseLibStuff();
  614.     }
  615.  
  616.     return( FALSE );
  617. }
  618.  
  619.  
  620. static
  621. void DeleteBasicResources( void )
  622. {
  623.     DeletePool( StringPool );
  624.     CloseLibStuff();
  625. }
  626.  
  627.  
  628. static
  629. BOOL OpenLibStuff( void )
  630. {
  631.     AttemptOpenLibrary( (&UtilityBase),                          NAME, "utility.library",     39UL );
  632.     AttemptOpenLibrary( (struct Library **)(&(GfxBase)),         NAME, "graphics.library",    39UL );
  633.     AttemptOpenLibrary( (struct Library **)(&(IntuitionBase)),   NAME, "intuition.library",   39UL );
  634.     AttemptOpenLibrary( (&GadToolsBase),                         NAME, "gadtools.library",    39UL );
  635.     AttemptOpenLibrary( (&DataTypesBase),                        NAME, "datatypes.library",   45UL );
  636.     AttemptOpenLibrary( (&IconBase),                             NAME, "icon.library",        39UL );
  637.     AttemptOpenLibrary( (&WorkbenchBase),                        NAME, "workbench.library",   39UL );
  638.     AttemptOpenLibrary( (&AslBase),                              NAME, "asl.library",         38UL );
  639.  
  640.     if( UtilityBase && GfxBase && IntuitionBase && GadToolsBase && DataTypesBase && IconBase && WorkbenchBase && AslBase  )
  641.     {
  642.       return( TRUE );
  643.     }
  644.  
  645.     CloseLibStuff();
  646.  
  647.     return( FALSE );
  648. }
  649.  
  650.  
  651. static
  652. void CloseLibStuff( void )
  653. {
  654.     CloseLibrary( AslBase );
  655.     CloseLibrary( WorkbenchBase );
  656.     CloseLibrary( IconBase );
  657.     CloseLibrary( DataTypesBase );
  658.     CloseLibrary( GadToolsBase );
  659.     CloseLibrary( (&(IntuitionBase -> LibNode)) );
  660.     CloseLibrary( (&(GfxBase -> LibNode)) );
  661.     CloseLibrary( UtilityBase );
  662. }
  663.  
  664.  
  665. static
  666. void RunTool( STRPTR srcname )
  667. {
  668.     STRPTR destname     = project . destname;
  669.     STRPTR datatypename = project . destdatatype;
  670.     BOOL   srcchanged   = FALSE;      /* srcname changed ? */
  671.     ULONG  srcsum       = SumString( srcname );
  672.  
  673.     if( project . srcname )
  674.     {
  675.       srcname = project . srcname;
  676.     }
  677.  
  678.     /* Check if datatype name is valid */
  679.     if( datatypename )
  680.     {
  681.       struct DataType *dtn;
  682.  
  683.       if( dtn = ObtainDataTypeA( DTST_RAM, (APTR)datatypename, NULL ) )
  684.       {
  685.         ReleaseDataType( dtn );
  686.       }
  687.       else
  688.       {
  689.         datatypename = NULL;
  690.       }
  691.     }
  692.  
  693.     /* GUI requested / required ? */
  694.     if( (project . gui) || (srcname == NULL) || (destname == NULL) || (datatypename == NULL) )
  695.     {
  696.       struct MsgPort *appport;
  697.       struct DTList  *dtl;
  698.       struct DTNode  *selecteddt;
  699.  
  700.       if( appport = CreateMsgPort() )
  701.       {
  702.         BPTR srclock = NULL;
  703.  
  704.         if( Strlen( srcname ) )
  705.         {
  706.           srclock = Lock( srcname, SHARED_LOCK );
  707.         }
  708.  
  709.         if( dtl = CreateDTDescrList( srclock, 0UL ) )
  710.         {
  711.           PubScreenName = project . pubscreen; /* set pubscreen name for GadToolsBox's windows */
  712.  
  713.           selecteddt = FindDTNodeByName( dtl, datatypename );
  714.  
  715.           if( !SetupScreen() )
  716.           {
  717.             struct FileRequester *SrcFileReq  = NULL,
  718.                                  *DestFileReq = NULL;
  719.  
  720.             if( !OpenDTConvert_MAINWindow() )
  721.             {
  722.               BOOL                 done             = FALSE;
  723.               struct IntuiMessage *imsg;
  724.               ULONG                sigmask;
  725.               BOOL                 get_source       = FALSE,
  726.                                    get_datatype     = FALSE,
  727.                                    get_destination  = FALSE;
  728.               BOOL                 convert          = FALSE;
  729.               BOOL                 convertable      = Strlen( srcname ) && selecteddt && Strlen( destname );
  730.               struct AppWindow    *appwindow        = NULL;
  731.               struct DiskObject   *appdobj          = NULL;
  732.               struct AppIcon      *appicon          = NULL;
  733.  
  734.               /* Running on WB screen ? */
  735.               if( ((DTConvert_MAINWnd -> WScreen -> Flags) & SCREENTYPE) == WBENCHSCREEN )
  736.               {
  737.                 appwindow = AddAppWindowA( 0UL, 0UL, DTConvert_MAINWnd, appport, NULL );
  738.               }
  739.               else
  740.               {
  741.                 if( appdobj = GetDefDiskObject( WBPROJECT ) )
  742.                 {
  743.                   /* Not running on workbench screen ? - Create AppIcon for wb app support ! */
  744.                   appicon = AddAppIconA( 0UL, 0UL, NAME, appport, NULL, appdobj, NULL );
  745.                 }
  746.               }
  747.  
  748.               GT_SetGadgetAttrs( DTConvert_MAINGadgets[ GD_SOURCE ],      DTConvert_MAINWnd, NULL, GTST_String, srcname,                                                  TAG_DONE );
  749.               GT_SetGadgetAttrs( DTConvert_MAINGadgets[ GD_DATATYPE ],    DTConvert_MAINWnd, NULL, GTTX_Text,   ((selecteddt)?(selecteddt -> dtn_Node . ln_Name):(NULL)), TAG_DONE );
  750.               GT_SetGadgetAttrs( DTConvert_MAINGadgets[ GD_DESTINATION ], DTConvert_MAINWnd, NULL, GTST_String, destname,                                                 TAG_DONE );
  751.               GT_SetGadgetAttrs( DTConvert_MAINGadgets[ GD_CONVERT ],     DTConvert_MAINWnd, NULL, GA_Disabled, (!convertable),                                           TAG_DONE );
  752.  
  753.               /* Main event loop */
  754.               while( !done )
  755.               {
  756.                 ULONG sigr;
  757.  
  758.                 sigmask = (1UL << (DTConvert_MAINWnd -> UserPort -> mp_SigBit)) | (1UL << (appport -> mp_SigBit));
  759.  
  760.                 if( DTConvert_DATATYPEWnd )
  761.                 {
  762.                   sigmask |= (1UL << (DTConvert_DATATYPEWnd -> UserPort -> mp_SigBit));
  763.                 }
  764.  
  765.                 sigr = Wait( sigmask );
  766.  
  767. /* Maiain window */
  768.                 if( sigr & (1UL << (DTConvert_MAINWnd -> UserPort -> mp_SigBit)) )
  769.                 {
  770.                   while( imsg = GT_GetIMsg( (DTConvert_MAINWnd -> UserPort) ) )
  771.                   {
  772.                     switch( imsg -> Class )
  773.                     {
  774.                       case IDCMP_CLOSEWINDOW:
  775.                       {
  776.                           done = TRUE;
  777.                       }
  778.                           break;
  779.  
  780.                       case IDCMP_REFRESHWINDOW:
  781.                       {
  782.                           GT_BeginRefresh( (imsg -> IDCMPWindow) );
  783.                           GT_EndRefresh( (imsg -> IDCMPWindow), TRUE );
  784.                       }
  785.                           break;
  786.  
  787.                       case IDCMP_GADGETUP:
  788.                       {
  789.                           switch( G( (imsg -> IAddress) ) -> GadgetID )
  790.                           {
  791.                             case GD_SOURCE: break;   /* NOP */
  792.  
  793.                             case GD_DATATYPE: break; /* NOP */
  794.  
  795.                             case GD_DESTINATION: break; /* NOP */
  796.  
  797.                             case GD_GET_SOURCE:
  798.                             {
  799.                                 get_source = TRUE;
  800.                             }
  801.                                 break;
  802.  
  803.                             case GD_GET_DATATYPE:
  804.                             {
  805.                                 get_datatype = TRUE;
  806.                             }
  807.                                 break;
  808.  
  809.                             case GD_GET_DESTINATION:
  810.                             {
  811.                                 get_destination = TRUE;
  812.                             }
  813.                                 break;
  814.  
  815.                             case GD_CONVERT:
  816.                             {
  817.                                 convert = TRUE;
  818.                             }
  819.                                 break;
  820.  
  821.                             case GD_CANCEL:
  822.                             {
  823.                                 done = TRUE;
  824.                             }
  825.                                 break;
  826.                           }
  827.                       }
  828.                           break;
  829.  
  830.                       case IDCMP_VANILLAKEY:
  831.                       {
  832.                           switch( imsg -> Code )
  833.                           {
  834.                             case 's': /* Source */
  835.                                 ActivateGadget( DTConvert_MAINGadgets[ GD_SOURCE ], DTConvert_MAINWnd, NULL );
  836.                                 break;
  837.  
  838.                             case 'S': /* Get Source */
  839.                                 get_source = TRUE;
  840.                                 break;
  841.  
  842.                             case 't': /* datatype */
  843.                             case 'T': /* datatype */
  844.                                 get_datatype = TRUE;
  845.                                 break;
  846.  
  847.                             case 'd': /* Destination */
  848.                                 ActivateGadget( DTConvert_MAINGadgets[ GD_DESTINATION ], DTConvert_MAINWnd, NULL );
  849.                                 break;
  850.  
  851.                             case 'D': /* Get Destination */
  852.                                 get_destination = TRUE;
  853.                                 break;
  854.  
  855.                             case 'c': /* Convert */
  856.                             case 'C': /* Convert */
  857.                             {
  858.                                 convert = TRUE;
  859.                             }
  860.                                 break;
  861.  
  862.                             case 'a': /* Cancel */
  863.                             case 'A': /* Cancel */
  864.                             {
  865.                                 done = TRUE;
  866.                             }
  867.                                 break;
  868.                           }
  869.                       }
  870.                           break;
  871.                     }
  872.  
  873.                     GT_ReplyIMsg( imsg );
  874.                   }
  875.                 }
  876.  
  877. /* DatataTypes requester */
  878.                 if( DTConvert_DATATYPEWnd )
  879.                 {
  880.                   if( sigr & (1UL << (DTConvert_DATATYPEWnd -> UserPort -> mp_SigBit)) )
  881.                   {
  882.                     BOOL  dtdone = FALSE;
  883.                     BOOL  dtok   = FALSE;
  884.                     LONG  oldselected,
  885.                           selected;
  886.  
  887.                     GT_GetGadgetAttrs( DTConvert_DATATYPEGadgets[ GD_DATATYPEDT ], DTConvert_DATATYPEWnd, NULL,
  888.                                        GTLV_Selected, (&oldselected),
  889.                                        TAG_DONE );
  890.  
  891.                     selected = oldselected;
  892.  
  893.                     while( imsg = GT_GetIMsg( (DTConvert_DATATYPEWnd -> UserPort) ) )
  894.                     {
  895.                       switch( imsg -> Class )
  896.                       {
  897.                         case IDCMP_CLOSEWINDOW:
  898.                         {
  899.                             dtdone = TRUE;
  900.                         }
  901.                             break;
  902.  
  903.                         case IDCMP_REFRESHWINDOW:
  904.                         {
  905.                             GT_BeginRefresh( (imsg -> IDCMPWindow) );
  906.                             GT_EndRefresh( (imsg -> IDCMPWindow), TRUE );
  907.                         }
  908.                             break;
  909.  
  910.                         case IDCMP_GADGETUP:
  911.                         {
  912.                             switch( G( (imsg -> IAddress) ) -> GadgetID )
  913.                             {
  914.                               case GD_DATATYPEDT: break; /* NOP */
  915.  
  916.                               case GD_OKDT:
  917.                               {
  918.                                   dtdone = dtok = TRUE;
  919.                               }
  920.                                   break;
  921.  
  922.                               case GD_CANCELDT:
  923.                               {
  924.                                   dtdone = TRUE;
  925.                               }
  926.                                   break;
  927.                             }
  928.                         }
  929.                             break;
  930.  
  931.                         case IDCMP_VANILLAKEY:
  932.                         {
  933.                             switch( imsg -> Code )
  934.                             {
  935.                               case 'o': /* Ok */
  936.                               case 'O': /* Ok */
  937.                                   dtdone = dtok = TRUE;
  938.                                   break;
  939.  
  940.                               case 'c': /* Cancel */
  941.                               case 'C': /* Cancel */
  942.                                   dtdone = TRUE;
  943.                                   break;
  944.  
  945.                               case 'd': /* DT Selection down */
  946.                               {
  947.                                   selected++;
  948.                               }
  949.                                   break;
  950.  
  951.                               case 'D': /* DT Selection up */
  952.                               {
  953.                                   selected--;
  954.                               }
  955.                                   break;
  956.                             }
  957.                         }
  958.                             break;
  959.  
  960.                         case IDCMP_RAWKEY:
  961.                         {
  962.                             switch( imsg -> Code )
  963.                             {
  964.                               case 76: /* up */
  965.                               {
  966.                                   if( (imsg -> Qualifier) & (IEQUALIFIER_LALT | IEQUALIFIER_RALT) )
  967.                                   {
  968.                                     selected = 0L; /* top */
  969.                                   }
  970.                                   else
  971.                                   {
  972.                                     if( (imsg -> Qualifier) & (IEQUALIFIER_LSHIFT | IEQUALIFIER_RSHIFT) )
  973.                                     {
  974.                                       selected -= 5L; /* page up */
  975.                                     }
  976.                                     else
  977.                                     {
  978.                                       selected--; /* previous entry */
  979.                                     }
  980.                                   }
  981.                               }
  982.                                   break;
  983.  
  984.                               case 77: /* down */
  985.                               {
  986.                                   if( (imsg -> Qualifier) & (IEQUALIFIER_LALT | IEQUALIFIER_RALT) )
  987.                                   {
  988.                                     selected = dtl -> dtl_NumNodes; /* bottom */
  989.                                   }
  990.                                   else
  991.                                   {
  992.                                     if( (imsg -> Qualifier) & (IEQUALIFIER_LSHIFT | IEQUALIFIER_RSHIFT) )
  993.                                     {
  994.                                       selected += 5L; /* page down */
  995.                                     }
  996.                                     else
  997.                                     {
  998.                                       selected++; /* next entry */
  999.                                     }
  1000.                                   }
  1001.                               }
  1002.                                   break;
  1003.                             }
  1004.                         }
  1005.                             break;
  1006.                       }
  1007.  
  1008.                       /* Check bounds */
  1009.                       if( selected < 0L )
  1010.                       {
  1011.                         selected = 0L;
  1012.                       }
  1013.                       else
  1014.                       {
  1015.                         if( selected > (LONG)(dtl -> dtl_NumNodes) )
  1016.                         {
  1017.                           selected = (LONG)(dtl -> dtl_NumNodes);
  1018.                         }
  1019.                       }
  1020.  
  1021.                       GT_ReplyIMsg( imsg );
  1022.                     }
  1023.  
  1024.                     if( selected != oldselected )
  1025.                     {
  1026.                       GT_SetGadgetAttrs( DTConvert_DATATYPEGadgets[ GD_DATATYPEDT ], DTConvert_DATATYPEWnd, NULL,
  1027.                                          GTLV_Selected,    selected,
  1028.                                          GTLV_MakeVisible, selected,
  1029.                                          TAG_DONE );
  1030.                     }
  1031.  
  1032.                     if( dtok )
  1033.                     {
  1034.                       if( selecteddt = (struct DTNode *)GetNumNode( (&(dtl -> dtl_List)), selected ) )
  1035.                       {
  1036.                         GT_SetGadgetAttrs( DTConvert_MAINGadgets[ GD_DATATYPE ], DTConvert_MAINWnd, NULL,
  1037.                                            GTTX_Text, (selecteddt -> dtn_Node . ln_Name),
  1038.                                            TAG_DONE );
  1039.                       }
  1040.                     }
  1041.  
  1042.                     if( dtdone )
  1043.                     {
  1044.                       CloseDTConvert_DATATYPEWindow();
  1045.                     }
  1046.                   }
  1047.                 }
  1048.  
  1049. /* appppwindow / appicon port */
  1050.                 if( sigr & (1UL << (appport -> mp_SigBit)) )
  1051.                 {
  1052.                   struct AppMessage *amsg;
  1053.  
  1054.                   while( amsg = (struct AppMessage *)GetMsg( appport ) )
  1055.                   {
  1056.                     switch( amsg -> am_Type )
  1057.                     {
  1058.                       case AMTYPE_APPWINDOW:
  1059.                       case AMTYPE_APPICON:
  1060.                       {
  1061.                           if( amsg -> am_NumArgs )
  1062.                           {
  1063.                             LockGUI();
  1064.  
  1065.                             if( (amsg -> am_NumArgs) == 1UL )
  1066.                             {
  1067.                               STRPTR name;
  1068.  
  1069.                               if( name = GetLockName( (amsg -> am_ArgList[ 0 ] . wa_Lock), (amsg -> am_ArgList[ 0 ] . wa_Name) ) )
  1070.                               {
  1071.                                 GT_SetGadgetAttrs( DTConvert_MAINGadgets[ GD_SOURCE ], DTConvert_MAINWnd, NULL, GTST_String, name, TAG_DONE );
  1072.  
  1073.                                 FreeVec( name );
  1074.                               }
  1075.                             }
  1076.                             else
  1077.                             {
  1078.                               ULONG i;
  1079.  
  1080.                               for( i = 0UL ; i < (amsg -> am_NumArgs) ; i++ )
  1081.                               {
  1082.                                 STRPTR name;
  1083.  
  1084.                                 if( name = GetLockName( (amsg -> am_ArgList[ i ] . wa_Lock), (amsg -> am_ArgList[ i ] . wa_Name) ) )
  1085.                                 {
  1086.                                   NewProject( name, ((selecteddt)?(selecteddt -> dtn_DTName):(NULL)), (project . iff), destname, TRUE, (project . pubscreen) );
  1087.  
  1088.                                   FreeVec( name );
  1089.                                 }
  1090.                               }
  1091.                             }
  1092.  
  1093.                             UnlockGUI();
  1094.                           }
  1095.                       }
  1096.                           break;
  1097.                     }
  1098.  
  1099.                     ReplyMsg( (&(amsg -> am_Message)) );
  1100.                   }
  1101.                 }
  1102.  
  1103. /* prorocess collected events */
  1104.                 if( get_source || get_datatype || get_destination )
  1105.                 {
  1106.                   LockGUI();
  1107.  
  1108.                   if( get_source )
  1109.                   {
  1110.                     if( SrcFileReq == NULL )
  1111.                     {
  1112.                       SrcFileReq = (struct FileRequester *)AllocAslRequestTags( ASL_FileRequest,
  1113.                                                                                 ASLFR_Screen,          (DTConvert_MAINWnd -> WScreen),
  1114.                                                                                 ASLFR_TitleText,       "Open File...",
  1115.                                                                                 ASLFR_InitialLeftEdge, ((DTConvert_MAINWnd -> LeftEdge) + (DTConvert_MAINWnd -> BorderLeft) + 1UL),
  1116.                                                                                 ASLFR_InitialTopEdge,  ((DTConvert_MAINWnd -> TopEdge)  + (DTConvert_MAINWnd -> BorderTop) + 1UL),
  1117.                                                                                 ASLFR_DoPatterns,      TRUE,
  1118.                                                                                 TAG_DONE );
  1119.                     }
  1120.  
  1121.                     if( SrcFileReq )
  1122.                     {
  1123.                       if( AslRequest( SrcFileReq, NULL ) )
  1124.                       {
  1125.                         ULONG  buffsize;
  1126.                         STRPTR filebuffer;
  1127.  
  1128.                         buffsize = Strlen( (SrcFileReq -> fr_Drawer) ) + Strlen( (SrcFileReq -> fr_File) ) + 10UL;
  1129.  
  1130.                         if( filebuffer = (STRPTR)AllocVec( buffsize, MEMF_PUBLIC ) )
  1131.                         {
  1132.                           strcpy( filebuffer, (SrcFileReq -> fr_Drawer) );
  1133.                           AddPart( filebuffer, (SrcFileReq -> fr_File), buffsize );
  1134.  
  1135.                           GT_SetGadgetAttrs( DTConvert_MAINGadgets[ GD_SOURCE ], DTConvert_MAINWnd, NULL, GTST_String, filebuffer, TAG_DONE );
  1136.  
  1137.                           FreeVec( filebuffer );
  1138.                         }
  1139.                       }
  1140.                     }
  1141.  
  1142.                     get_source = FALSE;
  1143.                   }
  1144.  
  1145.                   if( get_datatype )
  1146.                   {
  1147.                     if( DTConvert_DATATYPEWnd == NULL )
  1148.                     {
  1149.                       if( !OpenDTConvert_DATATYPEWindow() )
  1150.                       {
  1151.                         GT_SetGadgetAttrs( DTConvert_DATATYPEGadgets[ GD_DATATYPEDT ], DTConvert_DATATYPEWnd, NULL, GTLV_Labels, dtl, TAG_DONE );
  1152.                       }
  1153.                     }
  1154.  
  1155.                     get_datatype = FALSE;
  1156.                   }
  1157.  
  1158.                   if( get_destination )
  1159.                   {
  1160.                     if( DestFileReq == NULL )
  1161.                     {
  1162.                       DestFileReq = (struct FileRequester *)AllocAslRequestTags( ASL_FileRequest,
  1163.                                                                                  ASLFR_Screen,          (DTConvert_MAINWnd -> WScreen),
  1164.                                                                                  ASLFR_TitleText,       "Save File...",
  1165.                                                                                  ASLFR_InitialLeftEdge, ((DTConvert_MAINWnd -> LeftEdge) + (DTConvert_MAINWnd -> BorderLeft) + 1UL),
  1166.                                                                                  ASLFR_InitialTopEdge,  ((DTConvert_MAINWnd -> TopEdge)  + (DTConvert_MAINWnd -> BorderTop) + 1UL),
  1167.                                                                                  ASLFR_DoPatterns,      TRUE,
  1168.                                                                                  ASLFR_DoSaveMode,      TRUE,
  1169.                                                                                  TAG_DONE );
  1170.                     }
  1171.  
  1172.                     if( DestFileReq )
  1173.                     {
  1174.                       if( AslRequest( DestFileReq, NULL ) )
  1175.                       {
  1176.                         ULONG  buffsize;
  1177.                         STRPTR filebuffer;
  1178.  
  1179.                         buffsize = Strlen( (DestFileReq -> fr_Drawer) ) + Strlen( (DestFileReq -> fr_File) ) + 10UL;
  1180.  
  1181.                         if( filebuffer = (STRPTR)AllocVec( buffsize, MEMF_PUBLIC ) )
  1182.                         {
  1183.                           strcpy( filebuffer, (DestFileReq -> fr_Drawer) );
  1184.                           AddPart( filebuffer, (DestFileReq -> fr_File), buffsize );
  1185.  
  1186.                           GT_SetGadgetAttrs( DTConvert_MAINGadgets[ GD_DESTINATION ], DTConvert_MAINWnd, NULL, GTST_String, filebuffer, TAG_DONE );
  1187.  
  1188.                           FreeVec( filebuffer );
  1189.                         }
  1190.                       }
  1191.                     }
  1192.  
  1193.                     get_destination = FALSE;
  1194.                   }
  1195.  
  1196.                   UnlockGUI();
  1197.                 }
  1198.  
  1199. /* update */
  1200.                 GT_GetGadgetAttrs( DTConvert_MAINGadgets[ GD_SOURCE ], DTConvert_MAINWnd, NULL,
  1201.                                    GTST_String, (&srcname),
  1202.                                    TAG_DONE );
  1203.                                    
  1204.  
  1205.                 /* My quick hack to check if the src filename has been changed... */
  1206.                 srcchanged = (SumString( srcname ) != srcsum);
  1207.  
  1208.                 /* following block is experimental...  */
  1209.                 if( srcchanged )
  1210.                 {
  1211.                   struct DTList *old_dtl = dtl;
  1212.  
  1213.                   /* Update checksum... */
  1214.                   srcsum = SumString( srcname );
  1215.  
  1216.                   if( srclock )
  1217.                   {
  1218.                     UnLock( srclock );
  1219.                     srclock = NULL;
  1220.                   }
  1221.  
  1222.                   if( Strlen( srcname ) )
  1223.                   {
  1224.                     srclock = Lock( srcname, SHARED_LOCK );
  1225.                   }
  1226.  
  1227.                   dtl = CreateDTDescrList( srclock, 0UL );
  1228.  
  1229.                   if( selecteddt )
  1230.                   {
  1231.                     selecteddt = FindDTNodeByName( dtl, (selecteddt -> dtn_DTName) );
  1232.  
  1233.                     GT_SetGadgetAttrs( DTConvert_MAINGadgets[ GD_DATATYPE ], DTConvert_MAINWnd, NULL,
  1234.                                        GTTX_Text, ((selecteddt)?(selecteddt -> dtn_Node . ln_Name):("")),
  1235.                                        TAG_DONE );
  1236.                   }
  1237.  
  1238.                   /* update DataTypes selection gadget */
  1239.                   if( DTConvert_DATATYPEWnd )
  1240.                   {
  1241.                     GT_SetGadgetAttrs( DTConvert_DATATYPEGadgets[ GD_DATATYPEDT ], DTConvert_DATATYPEWnd, NULL, GTLV_Labels, dtl, TAG_DONE );
  1242.                   }
  1243.  
  1244.                   FreeDTDescrList( old_dtl );
  1245.                 }
  1246.  
  1247.                 GT_GetGadgetAttrs( DTConvert_MAINGadgets[ GD_DESTINATION ], DTConvert_MAINWnd, NULL,
  1248.                                    GTST_String, (&destname),
  1249.                                    TAG_DONE );
  1250.  
  1251.                 convertable = Strlen( srcname ) && selecteddt && Strlen( destname );
  1252.  
  1253.                 GT_SetGadgetAttrs( DTConvert_MAINGadgets[ GD_CONVERT ], DTConvert_MAINWnd, NULL,
  1254.                                    GA_Disabled, (!convertable),
  1255.                                    TAG_DONE );
  1256.  
  1257. /* run converter ? */
  1258.                 if( convert )
  1259.                 {
  1260.                   if( convertable )
  1261.                   {
  1262.                     LockGUI();
  1263.  
  1264.                     Message( "converting '%s' '%s' '%s'\n", srcname, (selecteddt -> dtn_DTName), destname );
  1265.  
  1266.                     Convert( srcname, (selecteddt -> dtn_DTName), FALSE, destname );
  1267.  
  1268.                     UnlockGUI();
  1269.                   }
  1270.  
  1271.                   convert = FALSE;
  1272.                 }
  1273.               }
  1274.  
  1275.               if( appwindow )
  1276.               {
  1277.                 (void)RemoveAppWindow( appwindow );
  1278.               }
  1279.  
  1280.               if( appdobj )
  1281.               {
  1282.                 FreeDiskObject( appdobj );
  1283.               }
  1284.  
  1285.               if( appicon )
  1286.               {
  1287.                 (void)RemoveAppIcon( appicon );
  1288.               }
  1289.  
  1290.               CloseDTConvert_DATATYPEWindow();
  1291.               CloseDTConvert_MAINWindow();
  1292.             }
  1293.  
  1294.             FreeAslRequest( SrcFileReq );
  1295.             FreeAslRequest( DestFileReq );
  1296.  
  1297.             CloseDownScreen();
  1298.           }
  1299.  
  1300.           FreeDTDescrList( dtl );
  1301.         }
  1302.  
  1303.         if( srclock )
  1304.         {
  1305.           UnLock( srclock );
  1306.         }
  1307.  
  1308.         ClearMsgPort( appport );
  1309.         DeleteMsgPort( appport );
  1310.       }
  1311.     }
  1312.     else
  1313.     {
  1314.       Convert( srcname, datatypename, (project . iff), destname );
  1315.     }
  1316.  
  1317.     if( IoErr() )
  1318.     {
  1319.       main_retval2 = IoErr();
  1320.       main_retval  = RETURN_ERROR;
  1321.     }
  1322. }
  1323.  
  1324.  
  1325. static
  1326. BOOL NewProject( STRPTR from, STRPTR datatype, BOOL iff, STRPTR destname, BOOL gui, STRPTR pubscreen )
  1327. {
  1328.     struct Process              *ThisProc = (struct Process *)FindTask( NULL );
  1329.     BPTR                         oldir    = CurrentDir( (ThisProc -> pr_HomeDir) );
  1330.     BOOL                         success  = FALSE;
  1331.     STRPTR                       buffer;
  1332.     ULONG                        buffersize;
  1333.     struct CommandLineInterface *cli = Cli();
  1334.  
  1335.     buffersize = Strlen( from ) + Strlen( datatype ) + Strlen( destname ) + Strlen( pubscreen ) + 512UL;
  1336.  
  1337.     if( buffer = (STRPTR)AllocVec( buffersize, MEMF_PUBLIC ) )
  1338.     {
  1339.       STRPTR next = buffer;
  1340.  
  1341.       if( cli )
  1342.       {
  1343.         STRPTR s = (STRPTR)BADDR( (cli -> cli_CommandName) );
  1344.  
  1345.         stccpy( next, (&s[ 1 ]), (int)(s[ 0 ] + 1U) );
  1346.       }
  1347.       else
  1348.       {
  1349.         strcpy( next, (ThisProc -> pr_Task . tc_Node . ln_Name) );
  1350.       }
  1351.  
  1352.       next += strlen( next );
  1353.  
  1354.       if( Strlen( from ) )
  1355.       {
  1356.         mysprintf( next, " FROM=\"%s\"", from );
  1357.         next += strlen( next );
  1358.       }
  1359.  
  1360.       if( Strlen( datatype ) )
  1361.       {
  1362.         mysprintf( next, " DATATYPE=\"%s\"", datatype );
  1363.         next += strlen( next );
  1364.       }
  1365.  
  1366.       if( iff )
  1367.       {
  1368.         mysprintf( next, " IFF" );
  1369.         next += strlen( next );
  1370.       }
  1371.  
  1372.       if( Strlen( destname ) )
  1373.       {
  1374.         mysprintf( next, " TO=\"%s\"", destname );
  1375.         next += strlen( next );
  1376.       }
  1377.  
  1378.       if( gui )
  1379.       {
  1380.         mysprintf( next, " GUI" );
  1381.         next += strlen( next );
  1382.       }
  1383.  
  1384.       if( Strlen( pubscreen ) )
  1385.       {
  1386.         mysprintf( next, " PUBSCREEN=\"%s\"", pubscreen );
  1387.       }
  1388.  
  1389.       if( SystemTags( buffer,
  1390.                       SYS_Input,  NULL,
  1391.                       SYS_Output, NULL,
  1392.                       SYS_Asynch, TRUE,
  1393.                       TAG_DONE ) != (-1L) )
  1394.       {
  1395.         success = TRUE;
  1396.       }
  1397.  
  1398.       FreeVec( buffer );
  1399.     }
  1400.  
  1401.     (void)CurrentDir( oldir );
  1402.  
  1403.     return( success );
  1404. }
  1405.  
  1406.  
  1407. /* GUI locking stuff */
  1408. static ULONG            GUILock                = 0UL;
  1409. static struct Requester MAINBlockReq           = { 0 };
  1410. static BOOL             MAINBlockReqActive     = FALSE;
  1411. static struct Requester DATATYPEBlockReq       = { 0 };
  1412. static BOOL             DATATYPEBlockReqActive = FALSE;
  1413.  
  1414.  
  1415. static
  1416. void LockGUI( void )
  1417. {
  1418.     if( GUILock++ == 0UL )
  1419.     {
  1420.       InitRequester( (&MAINBlockReq) );
  1421.       InitRequester( (&DATATYPEBlockReq) );
  1422.  
  1423.       if( DTConvert_MAINWnd )
  1424.       {
  1425.         SetWindowPointer( DTConvert_MAINWnd, WA_BusyPointer, TRUE, WA_PointerDelay, TRUE, TAG_DONE );
  1426.  
  1427.         if( MAINBlockReqActive == FALSE )
  1428.         {
  1429.           MAINBlockReqActive = Request( (&MAINBlockReq), DTConvert_MAINWnd );
  1430.         }
  1431.       }
  1432.  
  1433.       if( DTConvert_DATATYPEWnd )
  1434.       {
  1435.         SetWindowPointer( DTConvert_DATATYPEWnd, WA_BusyPointer, TRUE,  WA_PointerDelay, TRUE, TAG_DONE );
  1436.  
  1437.         if( DATATYPEBlockReqActive == FALSE )
  1438.         {
  1439.           DATATYPEBlockReqActive = Request( (&DATATYPEBlockReq), DTConvert_DATATYPEWnd );
  1440.         }
  1441.       }
  1442.     }
  1443. }
  1444.  
  1445.  
  1446. static
  1447. void UnlockGUI( void )
  1448. {
  1449.     if( --GUILock == 0UL )
  1450.     {
  1451.       if( DTConvert_MAINWnd )
  1452.       {
  1453.         SetWindowPointer( DTConvert_MAINWnd, WA_BusyPointer, FALSE, TAG_DONE );
  1454.  
  1455.         if( MAINBlockReqActive )
  1456.         {
  1457.           EndRequest( (&MAINBlockReq), DTConvert_MAINWnd );
  1458.           MAINBlockReqActive = FALSE;
  1459.         }
  1460.       }
  1461.  
  1462.       if( DTConvert_DATATYPEWnd )
  1463.       {
  1464.         SetWindowPointer( DTConvert_DATATYPEWnd, WA_BusyPointer, FALSE, TAG_DONE );
  1465.  
  1466.         if( DATATYPEBlockReqActive )
  1467.         {
  1468.           EndRequest( (&DATATYPEBlockReq), DTConvert_DATATYPEWnd );
  1469.           DATATYPEBlockReqActive = FALSE;
  1470.         }
  1471.       }
  1472.     }
  1473. }
  1474.  
  1475.  
  1476. void Message( STRPTR fmt, ... )
  1477. {
  1478.     VPrintf( fmt, (APTR)((&fmt) + 1) );
  1479. }
  1480.  
  1481.  
  1482. static
  1483. struct DTList *CreateDTDescrList( BPTR lock, ULONG groupid )
  1484. {
  1485.     APTR  pool;
  1486. #define NUM_GID_TABLE (32) /* we have much less GID_#? types defined yet (datatypes.library V45), but... */
  1487.     ULONG groupid_table[ NUM_GID_TABLE ] = { 0 };
  1488.  
  1489.     /* If a lock is given, we build here a list of all DT group ids the source file may be of... */
  1490.     if( lock )
  1491.     {
  1492.       struct DataType *dtn,
  1493.                       *prevdtn = NULL;
  1494.  
  1495.       /* Print whole list or determine the DataType of the file */
  1496.       while( dtn = ObtainDataType( DTST_FILE, (APTR)lock, DTA_DataType,                  prevdtn,
  1497.                                                           XTAG( groupid, DTA_GroupID ),  groupid,
  1498.                                                           TAG_DONE ) )
  1499.       {
  1500.         ULONG i,
  1501.               groupid = dtn -> dtn_Header -> dth_GroupID;
  1502.  
  1503.         /* Release previous DataType */
  1504.         ReleaseDataType( prevdtn );
  1505.  
  1506.         /* the follwoing statement assmes that GID_SYSTEM datatypes are not convertable
  1507.          * (and it filters some invalid datatypes which have a 0 group id)
  1508.          */
  1509.         if( (groupid != GID_SYSTEM) && (groupid != 0UL) )
  1510.         {
  1511.           for( i = 0UL ; i < NUM_GID_TABLE ; i++ )
  1512.           {
  1513.             if( !groupid_table[ i ] )
  1514.             {
  1515.               groupid_table[ i ] = groupid;
  1516.               break;
  1517.             }
  1518.           }
  1519.         }
  1520.  
  1521.         prevdtn = dtn;
  1522.       }
  1523.  
  1524.       /* Release last datatype */
  1525.       ReleaseDataType( prevdtn );
  1526.     }
  1527.  
  1528.     if( pool = CreatePool( (MEMF_PUBLIC | MEMF_CLEAR), 1024UL, 1024UL ) )
  1529.     {
  1530.       struct DTList *dtl;
  1531.  
  1532.       if( dtl = (struct DTList *)AllocVecPooled( pool, sizeof( struct DTList ) ) )
  1533.       {
  1534.         struct DataType *dtn,
  1535.                         *prevdtn = NULL;
  1536.         BOOL             error   = FALSE;
  1537.  
  1538.         NewList( (&(dtl -> dtl_List)) );
  1539.         dtl -> dtl_Pool = pool;
  1540.  
  1541.         /* Print whole list or determine the DataType of the file */
  1542.         while( dtn = ObtainDataType( DTST_RAM, NULL, DTA_DataType,                  prevdtn,
  1543.                                                      XTAG( groupid, DTA_GroupID ),  groupid,
  1544.                                                      TAG_DONE ) )
  1545.         {
  1546.           struct DataTypeHeader *dth;          /* datatypes header        */
  1547.           BOOL                   match = TRUE; /* include this datatype ? */
  1548.  
  1549.           /* Release previous DataType */
  1550.           ReleaseDataType( prevdtn );
  1551.  
  1552.           dth = dtn -> dtn_Header; /* Shortcut... */
  1553.  
  1554.           /* If we have a lock given, we previously build a group id table... */
  1555.           if( lock )
  1556.           {
  1557.             ULONG i;
  1558.  
  1559.             match = FALSE;
  1560.  
  1561.             /* ... now check if this datatype node matches any source groupid */
  1562.             for( i = 0UL ; ((i < NUM_GID_TABLE) && groupid_table[ i ]) ; i++ )
  1563.             {
  1564.               /* Match ? */
  1565.               if( groupid_table[ i ] == (dth -> dth_GroupID) )
  1566.               {
  1567.                 match = TRUE;
  1568.                 break;
  1569.               }
  1570.  
  1571.               /* Special kluge that we can convert GID_ANIMATION to GID_MOVIE and backwards
  1572.                * (both are based on animation.datatype, here we have the same interface :-)
  1573.                */
  1574.               if( ((groupid_table[ i ]   == GID_MOVIE) || (groupid_table[ i ]   == GID_ANIMATION)) &&
  1575.                   (((dth -> dth_GroupID) == GID_MOVIE) || ((dth -> dth_GroupID) == GID_ANIMATION)) )
  1576.               {
  1577.                 match = TRUE;
  1578.                 break;
  1579.               }
  1580.             }
  1581.           }
  1582.  
  1583.           /* Include this datatype ? */
  1584.           if( match )
  1585.           {
  1586.             STRPTR         groupid_name; /* short-cut to GID_#? name string    */
  1587.             ULONG          size;         /* size of struct DTNode and contents */
  1588.             struct DTNode *namenode;
  1589.  
  1590.             groupid_name = GetDTString( (dth -> dth_GroupID) );
  1591.  
  1592.             size = sizeof( struct DTNode ) + (strlen( (dth -> dth_Name) ) * 2UL) + Strlen( groupid_name ) + 16UL;
  1593.  
  1594.             /* Alloc and build node... */
  1595.             if( namenode = (struct DTNode *)AllocVecPooled( pool, size ) )
  1596.             {
  1597.               STRPTR s = (STRPTR)(namenode + 1);
  1598.  
  1599.               /* store listview names */
  1600.               mysprintf( s, "%s %s", (dth -> dth_Name), groupid_name );
  1601.               namenode -> dtn_Node . ln_Name = s;
  1602.  
  1603.               s = s + strlen( s ) + 2; /* next free space */
  1604.  
  1605.               /* store datatype name */
  1606.               strcpy( s, (dth -> dth_Name) );
  1607.               namenode -> dtn_DTName = s;
  1608.  
  1609.               /* add node */
  1610.               AddTail( (&(dtl -> dtl_List)), (&(namenode -> dtn_Node)) );
  1611.               dtl -> dtl_NumNodes++;
  1612.             }
  1613.             else
  1614.             {
  1615.               error = TRUE;
  1616.             }
  1617.           }
  1618.  
  1619.           prevdtn = dtn;
  1620.         }
  1621.  
  1622.         /* Release last datatype */
  1623.         ReleaseDataType( prevdtn );
  1624.  
  1625.         /* Success ? */
  1626.         if( !error )
  1627.         {
  1628.           return( dtl );
  1629.         }
  1630.       }
  1631.  
  1632.       DeletePool( pool );
  1633.     }
  1634.  
  1635.     return( NULL );
  1636. }
  1637.  
  1638.  
  1639. static
  1640. void FreeDTDescrList( struct DTList *list )
  1641. {
  1642.    if( list )
  1643.    {
  1644.      DeletePool( (list -> dtl_Pool) );
  1645.    }
  1646. }
  1647.  
  1648.  
  1649. static
  1650. struct DTNode *FindDTNodeByName( struct DTList *list, STRPTR name )
  1651. {
  1652.     struct DTNode *node;
  1653.     struct DTNode *result = NULL;
  1654.  
  1655.     if( list && name )
  1656.     {
  1657.       for( node = (struct DTNode *)(list -> dtl_List . lh_Head) ; node -> dtn_Node . ln_Succ ; node = (struct DTNode *)(node -> dtn_Node . ln_Succ) )
  1658.       {
  1659.         if( !Stricmp( (node -> dtn_DTName), name ) )
  1660.         {
  1661.           result = node;
  1662.           break;
  1663.         }
  1664.       }
  1665.     }
  1666.  
  1667.     return( result );
  1668. }
  1669.  
  1670.  
  1671. static
  1672. STRPTR StringSave( STRPTR s )
  1673. {
  1674.     STRPTR saved;
  1675.  
  1676.     if( s )
  1677.     {
  1678.       if( saved = AllocStringBuff( (ULONG)strlen( s ) ) )
  1679.       {
  1680.         strcpy( saved, s );
  1681.  
  1682.         return( saved );
  1683.       }
  1684.     }
  1685.  
  1686.     return( NULL );
  1687. }
  1688.  
  1689.  
  1690. static
  1691. STRPTR AllocStringBuff( ULONG size )
  1692. {
  1693.     return( (STRPTR)AllocVecPooled( StringPool, (size + 2UL) ) );
  1694. }
  1695.  
  1696.  
  1697. static
  1698. STRPTR UpdateString( STRPTR *storage, STRPTR newstring )
  1699. {
  1700.     STRPTR s;
  1701.  
  1702.     s = NULL;
  1703.  
  1704.     if( storage )
  1705.     {
  1706.       FreeString( (*storage) );
  1707.  
  1708.       s = (*storage) = StringSave( newstring );
  1709.     }
  1710.  
  1711.     return( s );
  1712. }
  1713.  
  1714.  
  1715. static
  1716. void FreeString( STRPTR s )
  1717. {
  1718.     if( s )
  1719.     {
  1720.       FreeVecPooled( StringPool, s );
  1721.     }
  1722. }
  1723.  
  1724.  
  1725. static
  1726. struct DiskObject *GetToolDiskObject( STRPTR name )
  1727. {
  1728.     struct DiskObject *dobj;
  1729.     struct Process    *ThisProc = (struct Process *)FindTask( NULL );
  1730.     BPTR               olddir   = CurrentDir( (ThisProc -> pr_HomeDir) );
  1731.  
  1732.     dobj = GetDiskObjectNew( name );
  1733.  
  1734.     (void)CurrentDir( olddir );
  1735.  
  1736.     return( dobj );
  1737. }
  1738.  
  1739.  
  1740. static
  1741. ULONG SumString( STRPTR s )
  1742. {
  1743.     ULONG sum = 0UL;
  1744.  
  1745.     if( s )
  1746.     {
  1747.       ULONG ch;
  1748.  
  1749.       while( ch = *s++ )
  1750.       {
  1751.         sum = (sum * 7UL) + ch;
  1752.       }
  1753.     }
  1754.  
  1755.     return( sum );
  1756. }
  1757.  
  1758.